﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StatusBarPanel.h"
#include "GmGuiTypeDefs.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

// StatusBarPanel 类实现
StatusBarPanel::StatusBarPanel(const std::wstring& name, int minWidth, int stretch, StatusBarPanelAlignment alignment)
    : m_name(name)
    , m_minWidth(minWidth)
    , m_stretch(stretch)
    , m_alignment(alignment)
{

}

bool StatusBarPanel::InsertControl(OwnerPtr<StatusBarCtrl> opCtrl, const StatusBarCtrl* pBefore)
{
    if(pBefore == nullptr)
    {
        m_panelCtrls.push_back(TransferOwnership(opCtrl));
        return true;
    }
    else
    {
        for(auto it = m_panelCtrls.begin(); it != m_panelCtrls.end(); ++it)
        {
            if((*it).get() == pBefore)
            {
                m_panelCtrls.insert(it, TransferOwnership(opCtrl));
                return true;
            }
        }
    }
    return false;
}

const StatusBarCtrl* StatusBarPanel::GetSubControl(int index) const
{
    if(index < 0 || index >= (int)m_panelCtrls.size())
        return nullptr;
    return m_panelCtrls[index].get();
}

const StatusBarCtrl* StatusBarPanel::GetSubControlByName(const std::wstring& ctrlName)
{
    if(ctrlName == L"")
        return nullptr;

    for(auto it = m_panelCtrls.begin(); it != m_panelCtrls.end(); ++it)
    {
        if((*it)->GetName() == ctrlName)
        {
            return (*it).get();
        }
    }
    return nullptr;
}

bool StatusBarPanel::RemoveControl(const StatusBarCtrl* pCtrl)
{
    if(pCtrl == nullptr)
        return false;

    for(auto it = m_panelCtrls.begin(); it != m_panelCtrls.end(); ++it)
    {
        if((*it).get() == pCtrl)
        {
            m_panelCtrls.erase(it);
            return true;
        }
    }
    return false;
}

bool StatusBarPanel::RemoveControlByName(const std::wstring& name)
{
    if(name == L"")
        return false;

    for(auto it = m_panelCtrls.begin(); it != m_panelCtrls.end(); ++it)
    {
        if((*it)->GetName() == name)
        {
            m_panelCtrls.erase(it);
            return true;
        }
    }
    return false;
}
